package com.suxin.config;

import com.github.pagehelper.QueryInterceptor;
import com.suxin.enums.BaseEnum;
import com.suxin.interceptor.MyInterceptor;
import com.suxin.utils.ClassUtil;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.List;

/**
 * @author Tang
 * @classname MybatisConfig
 * @description [  ]
 * @date 2021/5/11 14:11
 */
@ConditionalOnExpression(value = "'${mybatis.enum.package:null}' != 'null'")
@Configuration
public class MybatisConfig {

    @Value("${mybatis.enum.package:null}")
    private String enumPackage;

    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return (configuration) -> {
            configuration.addInterceptor(new MyInterceptor());
            // 6个参数的执行
            configuration.addInterceptor(new QueryInterceptor());
            TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
            List<Class<?>> allAssignedClass = ClassUtil.getAllAssignedClass(BaseEnum.class);
            System.out.println("注册配置类的枚举类列表为:" + Arrays.toString(allAssignedClass.toArray(new Class[]{})));
            allAssignedClass.forEach(item -> typeHandlerRegistry.register(item, new MyBaseTypeHandler(item)));
        };
    }
}